package com.yanqu.road.dao.impl.activity.dafuweng;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dafuweng.DFWUserCollectData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossDFWUserCollectDataDaoImpl extends TempDataObjectDao<DFWUserCollectData> {
    @Override
    protected boolean addData(DFWUserCollectData data) {
        String sql = "insert into t_c_activity_dfw_collect(activityId, groupId, unionUid, itemId, timeMark, userId, value) values(?,?,?,?,?,?,?)";
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getItemId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTimeMark()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getValue()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(DFWUserCollectData data) {
        String sql = "update t_c_activity_dfw_collect set value = ? where activityId = ? and groupId = ? and unionUid = ? and itemId = ? and timeMark = ? and userId = ?";
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getValue()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getItemId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTimeMark()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DFWUserCollectData parseData(ResultSet rs) throws SQLException {
        DFWUserCollectData data = new DFWUserCollectData();
        data.setActivityId(rs.getInt("activityId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setItemId(rs.getInt("itemId"));
        data.setTimeMark(rs.getInt("timeMark"));
        data.setUserId(rs.getLong("userId"));
        data.setValue(rs.getInt("value"));
        return data;
    }


    public Map<Integer, Map<String, Map<Integer, Map<Integer, Map<Long, DFWUserCollectData>>>>> getDFWUserCollectDataMap(int activityId, int groupId) {
        String sql = "select * from t_c_activity_dfw_collect where activityId = ? and groupId=?";
        Map<Integer, Map<String, Map<Integer, Map<Integer, Map<Long, DFWUserCollectData>>>>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DFWUserCollectData data = parse(rs);
                if (!map.containsKey(data.getActivityId())) {
                    map.put(data.getActivityId(), new ConcurrentHashMap<>());
                }
                if (!map.get(data.getActivityId()).containsKey(data.getUnionUid())) {
                    map.get(data.getActivityId()).put(data.getUnionUid(), new ConcurrentHashMap<>());
                }
                if (!map.get(data.getActivityId()).get(data.getUnionUid()).containsKey(data.getItemId())) {
                    map.get(data.getActivityId()).get(data.getUnionUid()).put(data.getItemId(), new ConcurrentHashMap<>());
                }
                if (!map.get(data.getActivityId()).get(data.getUnionUid()).get(data.getItemId()).containsKey(data.getTimeMark())) {
                    map.get(data.getActivityId()).get(data.getUnionUid()).get(data.getItemId()).put(data.getTimeMark(), new ConcurrentHashMap<>());
                }
                if (!map.get(data.getActivityId()).get(data.getUnionUid()).get(data.getItemId()).get(data.getTimeMark()).containsKey(data.getUserId())) {
                    map.get(data.getActivityId()).get(data.getUnionUid()).get(data.getItemId()).get(data.getTimeMark()).put(data.getUserId(), data);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
